<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>网上书城会员注册</title>
    <!-- shortcut icon导入ico图标 -->
    <link rel="shortcut icon" href="image/favicon.ico" />
    <!-- stylesheet导入css文件 -->
    <link rel="stylesheet" href="css/index.css" />
    <!-- 导入vue框架 -->
    <script src="js/vue.js"></script>
    <!-- 导入饿了么框架 -->
    <script src="js/index.js"></script>
    <!-- 导入axios异步框架 -->
    <script src="js/axios.min.js"></script>
    <style>
      #myForm {
        width: 600px;
        margin: 100px auto;
        /*居中*/
        background-color: white;
        border-radius: 3px;
        padding: 20px;
      }

      #myDate {
        width: 600px;
        margin: auto;
      }

      body {
        background: url("image/bg.jpg");
      }
    </style>
  </head>

  <body>
    <div id="app">
      <div id="myForm">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input show-password v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="telephone">
            <el-input v-model="ruleForm.telephone"></el-input>
          </el-form-item>

          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="ruleForm.gender">
              <el-radio-button label="1">男</el-radio-button>
              <el-radio-button label="0">女</el-radio-button>
              <el-radio-button label="2">保密</el-radio-button>
            </el-radio-group>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >注册</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <script>
      // 使用vue框架，
      var app = new Vue({
        el: "#app", // 要托管的根节点
        data() {
          return {
            now: new Date(),
            ruleForm: {
              username: null,
              password: null,
              telephone: null,
              gender: null,
            },
            rules: {
              username: [
                { required: true, message: "请输入用户名称", trigger: "blur" },
                {
                  min: 5,
                  max: 10,
                  message: "长度在 5 到 10 个字符",
                  trigger: "blur",
                },
              ],
              password: [
                { required: true, message: "请输入密码", trigger: "blur" },
                {
                  min: 5,
                  max: 10,
                  message: "长度在 5 到 10 个字符",
                  trigger: "blur",
                },
              ],
              telephone: [
                {
                  required: true,
                  trigger: "blur",
                  validator: (rule, value, callback) => {
                    if (!/^1[3456789]\d{9}$/.test(value)) {
                      callback(new Error("请输入正确的手机号"));
                    } else {
                      callback();
                    }
                  },
                },
                // { required: true, message: '请输入手机号', trigger: 'blur' },
                // { min: 11, max: 11, message: '请输入正确的手机号码', trigger: 'blur' }
              ],
              gender: [{ required: true, message: "请选择性别" }],
            },
          };
        }, // 数据区域，用于存储页面交互的数据变量
        methods: {
          submitForm(formName) {
            this.$refs[formName].validate((valid) => {
              if (valid) {
                axios
                  .post("http://127.0.0.1:8080/api/register", app.ruleForm)
                  .then(function (response) {
                    app.$message({
                      showClose: true,
                      message: "恭喜你，注册成功",
                      type: "success",
                      // 提示框关闭的时候会执行的函数
                      onClose: function () {
                        location = "login.html";
                      },
                    });
                  })
                  .catch(function (error) {
                    app.$message({
                      showClose: true,
                      message: "用户名已存在，注册失败",
                      type: "error",
                    });
                  });
                // 提交数据去后台注册保存
              } else {
                return false;
              }
            });
          },
          resetForm(formName) {
            this.$refs[formName].resetFields();
          },
        }, // 函数/方法，用户页面节点调用的函数
      });
    </script>
  </body>
</html>
